<template>
  <basic-container>
    <avue-crud :option="option"
               :data="data"
               :page="page"
               ref="crud"
               :permission="permissionList"
               @search-change="searchChange"
               @search-reset="searchReset"
               @selection-change="selectionChange"
               @current-change="currentChange"
               @size-change="sizeChange"
               @on-load="onLoad">
      <template slot="menuLeft">
        <el-button type="danger"
                   size="small"
                   icon="el-icon-delete"
                   v-if="permission.role_delete"
                   plain
                   @click="handleDelete">删 除
        </el-button>
      </template>
    </avue-crud>
  </basic-container>
</template>

<script>
import {
  getPages,
  remove
} from "@/api/system/log";
import { mapGetters } from "vuex";

export default {
  data() {
    return {
      selectionList: [],
        query:{},
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0
      },
      option: {
          searchMenuSpan: 4,
        tip: false,
        border: true,
        addBtn: false,
        index: true,
        menu: false,
        selection: true,
        column: [
          {
            label: "日志等级",
            prop: "level",
            width: "80px"
          },
          {
            label: "消息内容",
            prop: "message",
			searchSpan:4,
            search: true,
            width: "130px"
          },
          {
            label: "请求Url",
            prop: "callsite",
			searchSpan:4,
            search: true,
            width: "150px"
          },
          {
            label: "IP地址",
            prop: "ip",
			searchSpan:4,
            search: true,
            width: "120px"
          },
          {
            label: "浏览器",
            prop: "browser"
          },
          {
            label: "操作用户",
            prop: "user",
            width: "100px"
          },
          {
            label: "日志时间",
            prop: "logged",
            type: "daterange",
            width: "160px"
          }
        ]
      },
      data: []
    };
  },
  computed: {
    ...mapGetters(["permission"]),
    permissionList() {
      return {
        delBtn: this.vaildData(this.permission.role_delete, false)
      };
    },
    ids() {
      let ids = [];
      this.selectionList.forEach(ele => {
        ids.push(ele.id);
      });
      return ids.join(",");
    }
  },
  methods: {
    searchReset() {
      this.query={};
      this.page.currentPage=1;
      this.onLoad(this.page);
    },
    searchChange(params,done) {
      this.page.currentPage=1;
      this.query=params;
      this.onLoad(this.page, params);
      done();
    },
    selectionChange(list) {
      this.selectionList = list;
    },
    handleDelete() {
      if (this.selectionList.length === 0) {
        this.$message.warning("请选择至少一条数据");
        return;
      }
      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          return remove(this.ids);
        })
        .then(() => {
          this.onLoad(this.page);
          this.$refs.crud.toggleSelection();
        });
    },
    currentChange(currentPage){
      this.page.currentPage = currentPage;
    },
    sizeChange(pageSize){
      this.page.pageSize = pageSize;
    },
    onLoad(page, params = {}) {
      getPages(page.currentPage, page.pageSize, Object.assign(params, this.query)).then(res => {
        const data = res.data
        this.data = data.data;
        this.page.total = data.remark;
      });
    }
  }
};
</script>

<style>
</style>
